/*
 * Copyright 2009 Original Author(s)
 * 
 * This file is part of Kommando
 * 
 * Kommando is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Kommando is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Kommando.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kommando.ui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.Icon;

import org.kommando.core.util.Assert;

/**
 * Wraps another {@link Icon}, and puts padding round it so it has the desired size.
 * 
 * @author Peter De Bruycker
 */
public class PaddingIcon implements Icon {

    private Icon delegate;
    private Dimension size;

    public PaddingIcon(Icon delegate, Dimension size) {
        Assert.argumentNotNull("delegate", delegate);
        Assert.argumentNotNull("size", size);

        if(!(delegate.getIconWidth() <= size.width
                && delegate.getIconHeight() <= size.height)) {
            System.out.println("stop!!!");
        }
        
        Assert.isTrue("size of delegate must be smaller than the target size", delegate.getIconWidth() <= size.width
                && delegate.getIconHeight() <= size.height);

        this.delegate = delegate;
        this.size = size;
    }

    @Override
    public int getIconHeight() {
        return size.height;
    }

    @Override
    public int getIconWidth() {
        return size.width;
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        int xOffset = (size.width - delegate.getIconWidth()) / 2;
        int yOffset = (size.height - delegate.getIconHeight()) / 2;

        delegate.paintIcon(c, g, x + xOffset, y + yOffset);
    }
}
